﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;


namespace BD2MKV
{
    public partial class FormShow : Form
    {
        public string label = null;
        public string seriesId = null;
        public string language = null;
        public string seriesName = null;

        
        private Dictionary<string, string> series = new Dictionary<string,string>();

        public FormShow()
        {
            InitializeComponent();
        }

        private void listViewShows_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewShows.SelectedItems.Count > 0)
            {
                textBoxDescription.Text = series[listViewShows.SelectedItems[0].Text];
                buttonOk.Enabled = true;
                
                seriesId = listViewShows.SelectedItems[0].Text;
                seriesName = listViewShows.SelectedItems[0].SubItems[1].Text;
                language = listViewShows.SelectedItems[0].SubItems[2].Tag.ToString();
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            listViewShows.Items.Clear();

            
            XmlTextReader reader = new XmlTextReader("http://www.thetvdb.com/api/GetSeries.php?seriesname=" + textBoxShow.Text);
            Console.WriteLine(label);
            string tmpSeriesId = "";
            string tmpLanguage = "";
            string tmpSeriesName = "";
            string tmpOverview = "";
            string tmpFirstAired = "";

            try
            {
                reader.Read();
            }
            catch (Exception ex)
            {
                textBoxDescription.Text = ex.Message.ToString();
                return;
            }

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element: // The node is an element.
                        Console.Write("<" + reader.Name);
                        Console.WriteLine(">");

                        switch (reader.Name)
                        {
                            case "seriesid":
                                reader.Read(); // Series Id
                                tmpSeriesId = reader.Value;
                                Console.WriteLine(reader.Value);
                                break;
                            case "language":
                                reader.Read(); // language
                                tmpLanguage = reader.Value;
                                Console.WriteLine(reader.Value);
                                break;
                            case "SeriesName":
                                reader.Read(); // Series Name
                                tmpSeriesName = reader.Value;
                                Console.WriteLine(reader.Value);
                                break;
                            case "Overview":
                                reader.Read(); // Overview
                                tmpOverview = reader.Value;
                                Console.WriteLine(reader.Value);
                                break;
                            case "FirstAired":
                                reader.Read(); // First Aired
                                tmpFirstAired = reader.Value;
                                Console.WriteLine(reader.Value);
                                break;
                            default:
                                break;
                        }
                        
                        break;
                    case XmlNodeType.Text: //Display the text in each element.
                        Console.WriteLine(reader.Value);
                        break;
                    case XmlNodeType.EndElement: //Display the end of the element.
                        switch (reader.Name)
                        {
                            case "Series":
                                series[tmpSeriesId] = tmpOverview;

                                ListViewItem listViewItem1 = new ListViewItem();
                                listViewItem1.Text = tmpSeriesId;
                                listViewItem1.SubItems.Add(tmpSeriesName);
                                listViewItem1.SubItems.Add(LanguageCodes.GetName(tmpLanguage));
                                listViewItem1.SubItems[2].Tag = tmpLanguage;
                                listViewItem1.SubItems.Add(tmpFirstAired);
                                listViewShows.Items.Add(listViewItem1);

                                listViewShows.Refresh();

                                tmpSeriesId = "";
                                tmpLanguage = "";
                                tmpSeriesName = "";
                                tmpOverview = "";
                                tmpFirstAired = "";

                                break;
                            default:
                                break;
                        }
                        Console.Write("</" + reader.Name);
                        Console.WriteLine(">");
                        break;
                    default:
                        Console.WriteLine(reader.Value);
                        break;
                }
            }           
        }

        private void FormShow_Load(object sender, EventArgs e)
        {
            textBoxShow.Text = label;
            buttonSearch_Click(this, EventArgs.Empty);
            
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            //TheTVDB.LoadSeries(seriesId, language);
        }


        
    }
}
